package sw.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import sw.entity.Classes;
import sw.repo.ClassesRepo;
import sw.repo.CollegeRepo;

@Controller
public class ClassesController {
	@Autowired
	private ClassesRepo classesRepository;
	@Autowired
	private CollegeRepo collegeRepository;
	
	@GetMapping("/classes") //显示班级列表视图
	public String showClassesPage(Model model){
		Iterable<Classes> classes=classesRepository.findAll();
		model.addAttribute("classes",classes);
		return "classes/index";
	}
	
	@GetMapping("/classes/form")
	public String showClassesAddPage(Model model){
		model.addAttribute("colleges",collegeRepository.findAll());
		//把学院列表传输到前端下拉选择栏
		return "classes/form";
	}
	
	@PostMapping("/classes/classes")  //接收前端数据
	public String showForm(Classes classes){
		classesRepository.save(classes);
		return "redirect:/classes";
	}
}
